package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Article;
import com.example.shoppingmall.pojo.Articlelist;
import com.example.shoppingmall.service.ArticleService;
import com.example.shoppingmall.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/Article")
public class ArticlelistController {
    @Autowired
    ArticleService articleService;

    @Autowired
    ArticleTypeService articleTypeService;

    @RequestMapping("/Articlelist")
    public String Articlelist(@RequestParam(value = "create",required = false) String create, @RequestParam(value = "upddate",required = false) String upddate,
                              @RequestParam(value = "status",required = false) String status, @RequestParam(value = "sorts",required = false) String sorts,
                              @RequestParam(value = "updatedate",required = false) String updatedate, @RequestParam(value = "title",required = false) String title,
                              @RequestParam(value = "releaseStatus",required = false) String releaseStatus, Model model,
                              @RequestParam(value = "pageno",defaultValue = "1") String pageno, @RequestParam(value = "pageSize",defaultValue = "6") String pageSize){
        if(releaseStatus==null){
            releaseStatus="0";
        }
        int count=articleService.allcount(updatedate,title,releaseStatus);
        int num=(Integer.valueOf(pageno)-1)*Integer.valueOf(pageSize);
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageCount(count);
        page.setPageSize(Integer.valueOf(pageSize));
        List<Articlelist> articlelists=articleService.getarticlelistall(create, upddate, status, sorts, updatedate, title, releaseStatus, num, Integer.valueOf(pageSize));
        List<Article> artic=articleTypeService.getarticlelistsall();
        model.addAttribute("artic",artic);
        model.addAttribute("page",page);
        model.addAttribute("articlelists",articlelists);
        model.addAttribute("updatedate",updatedate);
        model.addAttribute("title",title);
        return "Operate/Articlelist";
    }

    //删除操作
    @MyLog(value = "运营管理->文章列表->删除")
    @RequestMapping("/delarticle")
    @ResponseBody
    public Map<Object,Object> delarticle(@RequestParam("id") Integer id){
        Map<Object,Object> map=new HashMap<>();
        if(articleService.delarticle(id)>0){
            map.put("dels","yes");
        }else{
            map.put("dels","no");
        }
        return map;
    }

    //查看详情
    @MyLog(value = "运营管理->文章列表->查看详情")
    @RequestMapping("/selarticle")
    public String selarticle(Integer id,Model model){
        Articlelist articlelist=articleService.selarticle(id);
        model.addAttribute("articlelist",articlelist);
        List<Article> articles=articleTypeService.getarticlelistsall();
        model.addAttribute("articles",articles);
        return "Operate/ArticleSelected";
    }

    //修改操作
    @MyLog(value = "运营管理->文章列表->修改")
    @RequestMapping("/updarticle")
    public String updarticle(@RequestParam("articletype") String articletype, @RequestParam("title") String title,
                             @RequestParam("content") String content,
                             @RequestParam("releaseStatus") String releaseStatus, @RequestParam("sort") String sort,
                             @RequestParam("id") String id) {
            Articlelist articlelist = new Articlelist();
            articlelist.setId(Integer.valueOf(id));
            articlelist.setArticletype(Integer.valueOf(articletype));
            articlelist.setTitle(title);
            articlelist.setConcent(content);
            articlelist.setReleaseStatus(Integer.valueOf(releaseStatus));
            articlelist.setSort(Integer.valueOf(sort));
            if (articleService.updarticle(articlelist) > 0) {
                return "redirect:/Article/Articlelist";
            }else{
                return "error";
        }
    }

    //新增文章
    @MyLog(value = "运营管理->文章列表->添加")
    @RequestMapping("/addarticle")
    public String addarticle(@RequestParam("articletype") String articletype, @RequestParam("title") String title,
                             @RequestParam("content") String content,
                             @RequestParam("releaseStatus") String releaseStatus, @RequestParam("sort") String sort){
            Articlelist articlelist = new Articlelist();
            articlelist.setArticletype(Integer.valueOf(articletype));
            articlelist.setTitle(title);
            articlelist.setConcent(content);
            articlelist.setReleaseStatus(Integer.valueOf(releaseStatus));
            articlelist.setSort(Integer.valueOf(sort));
            if (articleService.addarticle(articlelist) > 0) {
                return "redirect:/Article/Articlelist";
            }else{
                return "error";
            }
    }
}
